package com.along.dev.rbac.mapper;


import com.along.dev.rbac.dto.RoleDTO;
import com.along.dev.rbac.entity.Role;
import com.along.dev.rbac.qo.RoleQO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
@Mapper
public interface RoleMapper {

    @Results(id = "rbacRoleMap", value = {
            @Result(property = "roleId", column = "role_id"),
            @Result(property = "roleName", column = "role_name"),
            @Result(property = "roleKey", column = "role_key"),
            @Result(property = "roleSort", column = "role_sort"),
            @Result(property = "dataScope", column = "data_scope"),
            @Result(property = "menuCheckStrictly", column = "menu_check_strictly"),
            @Result(property = "deptCheckStrictly", column = "dept_check_strictly"),
            @Result(property = "status", column = "status"),
            @Result(property = "delFlag", column = "del_flag"),
            @Result(property = "createBy", column = "create_by"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateBy", column = "update_by"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "remark", column = "remark")
    })
    @Select({"<script>",
            "SELECT role_id, role_name, role_key, role_sort, data_scope, menu_check_strictly, dept_check_strictly, status, del_flag, create_by, update_by, create_time, update_time,remark FROM sys_role",
            "<where> ",
            "<if test='roleId != null'> AND role_id = #{roleId} </if>",
            "<if test='roleName != null'> AND role_name like  '%' || #{roleName} || '%' </if>",
            "<if test='status != null'> AND status = #{status} </if>",
            "</where>",
            "order by role_sort, data_scope",
            "</script>"})
    Page<Role> find(RoleQO roleQO);

    @Delete("DELETE FROM sys_role WHERE role_id = #{roleId}")
    int delete(Integer roleId);

    @Update({"<script>",
            "UPDATE sys_role SET",
            "role_name = #{roleName, jdbcType=VARCHAR},",
            "role_key = #{roleKey, jdbcType=VARCHAR},",
            "role_sort = #{roleSort, jdbcType=VARCHAR},",
            "data_scope = #{dataScope, jdbcType=INTEGER},",
            "menu_check_strictly = #{menuCheckStrictly, jdbcType=VARCHAR},",
            "dept_check_strictly = #{deptCheckStrictly, jdbcType=VARCHAR},",
            "status = #{status, jdbcType=VARCHAR},",
            "del_flag = #{delFlag, jdbcType=VARCHAR},",
            "create_by = #{createBy, jdbcType=VARCHAR},",
            "create_time = #{createTime, jdbcType=TIMESTAMP},",
            "update_by = #{updateBy, jdbcType=VARCHAR},",
            "update_time = #{updateTime, jdbcType=TIMESTAMP}",
            "WHERE role_id = #{roleId}",
            "</script>"
    })
    int update(Role role);

    @Insert({"<script>",
            "INSERT INTO sys_role (",
            "role_id, role_name, role_key, role_sort, data_scope, ",
            "menu_check_strictly, dept_check_strictly, status, del_flag, ",
            "create_by, create_time, update_by, update_time",
            ") VALUES (",
            "#{roleId, jdbcType=INTEGER}, ",
            "#{roleName, jdbcType=VARCHAR}, ",
            "#{roleKey, jdbcType=VARCHAR}, ",
            "#{roleSort, jdbcType=INTEGER}, ", // INTEGER
            "#{dataScope, jdbcType=INTEGER}, ",
            "#{menuCheckStrictly, jdbcType=VARCHAR}, ",
            "#{deptCheckStrictly, jdbcType=VARCHAR}, ",
            "#{status, jdbcType=VARCHAR}, ",
            "#{delFlag, jdbcType=VARCHAR}, ",
            "#{createBy, jdbcType=VARCHAR}, ",
            "now(), ",
            "#{updateBy, jdbcType=VARCHAR}, ",
            "now()",
            ")",
            "</script>"
    })
    int save(Role role);
}
